package com.wolfpire.workflow.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.workflow.dao.OperateDao;
import com.wolfpire.workflow.em.WorkflowStatus;
import com.wolfpire.workflow.om.NodeOperate;
import com.wolfpire.workflow.om.Operate;

@Repository("operateDao")
public class OperateDaoImpl extends BaseHibernateDao<Operate, Long> implements OperateDao {

	
	@SuppressWarnings("unchecked")
	public List<Operate> queryOperateList(Long workFlowId) {
		Criteria criteria = createCriteria(Restrictions.eq("status", WorkflowStatus.NORMAL.getIndex()));
		if(workFlowId != null){
			criteria.add(Property.forName("workFlowId").in(new Object[]{1L, workFlowId}));
		}
		criteria.addOrder(Order.desc("workFlowId"));
		return criteria.list();
	}
	
	public void saveNodeOperate(NodeOperate nodeOperate){
		String sql = "insert into WF_NODE_OPERATE (NODE_ID, OPERATE_ID, ANOTHER_NAME) values (?, ?, ?)";
		
		Query query = getSession().createSQLQuery(sql);
		query.setParameter(0, nodeOperate.getNodeId());
		query.setParameter(1, nodeOperate.getOperateId());
		query.setParameter(2, nodeOperate.getAnotherName());
		query.executeUpdate();
	}
	
	public void deleteNodeOperate (Integer nodeId) {
		String sql = "delete  WF_NODE_OPERATE T where T.NODE_ID=?";
		
		Query query = getSession().createSQLQuery(sql);
		query.setParameter(0, nodeId);
		query.executeUpdate();
	}

	@Override
	public Page<Operate> findOperates(EasyuiPage<Operate> page, Operate filterOperate) {
		Criterion[] criterions = createFilter(filterOperate);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = createCriteria(criterions);
			c = this.setPageParameter(c, page);
			@SuppressWarnings({ "unchecked" })
			List<Operate> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	private Criterion[] createFilter(Operate filterOperate) {
		if (null == filterOperate) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterOperate.getOperateId()) {
			criterions.add(Restrictions.eq("operateId", filterOperate.getOperateId()));
		}
		if (!StringUtils.isBlank(filterOperate.getOperateName())) {
			criterions.add(Restrictions.eq("operateName", filterOperate.getOperateName()));
		}
		if (!StringUtils.isBlank(filterOperate.getOperateEvent())) {
			criterions.add(Restrictions.eq("operateEvent", filterOperate.getOperateEvent()));
		}
		if (!StringUtils.isBlank(filterOperate.getStatus())) {
			criterions.add(Restrictions.eq("status", filterOperate.getStatus()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Operate> queryNodeOperateList(Long nodeId) {
		// TODO Auto-generated method stub
		StringBuffer querySql = new StringBuffer(512);
		querySql.append(" SELECT O.* FROM WF_NODE_OPERATE WNO, WF_OPERATE O ")
				.append(" WHERE WNO.OPERATE_ID = O.OPERATE_ID ")
				.append(" AND O.STATUS=:status ")
				.append(" AND WNO.NODE_ID=:nodeId");
		List<String> propertyNames = new ArrayList<String>();
		propertyNames.add("status");
		propertyNames.add("nodeId");
		List propertyValues = new ArrayList();
		propertyValues.add("1");
		propertyValues.add(nodeId);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames , propertyValues).addEntity(Operate.class);
		return query.list();
	}
	

}
